#!/bin/sh

#某个日期在一年当中的天数,全局变量
num_date_days=0
#某一个年份全年的天数
num_year_days=0
#两个日期相差的天数
diff_between_dates=0
#当前日期
today=`date "+%Y-%m-%d"`
#需要处理的日期间隔期限
days_diff_standard=3
#打包得到的目标文件名
REALEASE_TARGETFILENAME="/opt/MyVMBackUp_$today.zip"

function get_date_days()
{
	
	year=`echo $1|cut -d '-' -f 1`
	month=`echo $1|cut -d '-' -f 2`
	day=`echo $1|cut -d '-' -f 3`

	#echo "年份:$year,月份:$month,日期:$day"
	month_temp=1
	num_date_days=$day
	while [ $month_temp -lt $month ]
	do

	case  $month_temp in
	    1|3|5|7|8|10|12)   num_date_days=`expr 31 + $num_date_days` ;;
	    4|6|9|11)  num_date_days=`expr 30 + $num_date_days` ;;
	    2) if [ `expr $year % 100` != 0 ] && [ `expr $year % 4` == 0 ] || [ `expr $year % 400` == 0 ];then
		  num_date_days=`expr 29 + $num_date_days`
		else
		  num_date_days=`expr 28 + $num_date_days`
		fi ;;
	esac
	month_temp=$(($month_temp + 1))
	done
	return $num_date_days
}

#获取某一个年份的天数
function get_year_daysnum()
{
	
	year=$1
	

# 	echo "年份:$year"
	month_temp=1
	num_year_days=0
	while [ $month_temp -le 12 ]
	do
# 		echo "计算$month_temp月份的天数"
		case  $month_temp in
		    1|3|5|7|8|10|12)   num_year_days=`expr 31 + $num_year_days` ;;
		    4|6|9|11)  num_year_days=`expr 30 + $num_year_days` ;;
		    2) if [ `expr $year % 100` != 0 ] && [ `expr $year % 4` == 0 ] || [ `expr $year % 400` == 0 ];then
			  num_year_days=`expr 29 + $num_year_days`
			else
			  num_year_days=`expr 28 + $num_year_days`
			fi ;;
		esac
	month_temp=$(($month_temp + 1))
	done
# 	echo "天数:$num_year_days"
	return $num_year_days
}

#获取两个%04d-%02d-%02d形式的日期之间的天数差,前一个日期大于后一个日期,则返回正值,相等则返回0,小于则返回赋值
function get_date_diff()
{
    diff_between_dates=0 
    year1=`echo $1|cut -d '-' -f 1`
    year2=`echo $2|cut -d '-' -f 1`
    
    get_date_days $1
    num_date_days1=$num_date_days
#     echo $num_date_days1
    
    get_date_days $2
    num_date_days2=$num_date_days
#     echo $num_date_days2
    
    if [ $year1 -eq $year2 ];then
# 		echo "年份$year1和年份$year2相等"
		diff_between_dates=`expr $num_date_days1 - $num_date_days2`
    elif [ $year1 -gt $year2 ];then
# 		echo "年份$year1大于年份$year2"
		get_year_daysnum $year2
		diff_between_dates=`expr $num_year_days - $num_date_days2`
		year_i=`expr $year2 + 1`
		
		
		while [ $year_i -lt $year1 ]
		do
			get_year_daysnum $year_i
		        diff_between_dates=`expr $diff_between_dates + $num_year_days`
		        
		        year_i=$(($year_i + 1))
		done
		
		diff_between_dates=`expr $diff_between_dates + $num_date_days1`
    elif [ $year2 -gt $year1 ];then
# 		echo "年份$year1小于年份$year2"
		get_year_daysnum $year1
		diff_between_dates=`expr $num_year_days - $num_date_days1`
		year_i=`expr $year1 + 1`
		
		
		while [ $year_i -lt $year2 ]
		do
			get_year_daysnum $year_i
		        diff_between_dates=`expr $diff_between_dates + $num_year_days`
		        
		        year_i=$(($year_i + 1))
		done
		
		diff_between_dates=`expr $diff_between_dates + $num_date_days2`	
		diff_between_dates=`expr 0 - $diff_between_dates`
		
    fi	
    
#     get_year_daysnum 2014
#     echo $num_year_days
#     echo "得到的天数:$day_count"
#     echo "日期$1和日期$2相差天数:$diff_between_dates"
    
}




function recurse_search()
{
  # 'cd' down into the named directory
  cd $1

  # Iterate through all of the files
  for file in * ; do

    # If the file is a directory, recurse
    if [ -d "$file" ] ; then
	  fullfilename="`pwd`/$file"
	  recurse_search $fullfilename
    fi

    if [ -f "$file" ] ; then

#       longfile=`ls -l --time-style=long-iso $file`
#       filedate= ${`ls -l --time-style=long-iso $file | cut -d ' ' -f 6 `}
	  filedate=`ls -l --time-style=long-iso $file |cut -d ' ' -f 6 `
      
	  get_date_diff $today $filedate
	  if [ $diff_between_dates -le  $days_diff_standard ] ; then
		      fullfilename="`pwd`/$file"
	      	      echo  "文件 [ $fullfilename ] 的日期: $filedate 怎么跟 $today 相差 $diff_between_dates 天"
	      	      echo $REALEASE_TARGETFILENAME
	      	      zip -m  $REALEASE_TARGETFILENAME $fullfilename
	      	      
	  fi     
      
    fi
  done

  if [ "$1" != "." ] ; then
    cd ..
  fi

}
echo "日期:$today生成的系统备份文件,最近$days_diff_standard以内的文件都会被备份" >> ./log.txt
zip  -m $REALEASE_TARGETFILENAME ./log.txt
recurse_search $1
exit

